$(function () {

    //begin列表信息
    $("#recruitmentPlan_datagrid").datagrid({
        url: "/recruitmentPlan/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100, hidden: true},
            {field: "planType", title: "计划类型", width: 100},
            {field: "plantime", title: "计划时间", width: 100},
            {field: "runtime", title: "实行时间", width: 100},
            {
                field: "inputUser", title: "审核人", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.username;
                } else {
                    return "未审核";
                }
            }
            },
            {field: "node", title: "备注", width: 100}
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber: 1,
        pageSize: 3,
        pageList: [3, 5, 10, 20],
        toolbar: "#recruitmentPlan_tb"
    });
    //end列表信息
    //选择明细的列表
    $("#employeeNeeds").datagrid({
        iconCls: 'icon-edit',
        method: 'get',
        url: 'employeeNeeds/selectUnreviewed',
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 70, hidden: true},
            {field: "inputUser", title: "录入人", width: 70, formatter: function (value, row, index) {
                if (value) {
                    return value.username;
                } else {
                    return "暂未查询";
                }
            }},
            {
                field: "department", title: "申请部门", width: 70, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                } else {
                    return "未分配";
                }
            }
            },
            {
                field: "applicant", title: "申请人", width: 70, formatter: function (value, row, index) {
                if (value) {
                    return value.username;
                } else {
                    return "暂未查询";
                }
            }
            },
            {field: "begindate", title: "开始时间", width: 80},
            {field: "enddate", title: "结束时间", width: 80},
            {field: "inputdate", title: "录入时间", width: 85}
        ]]
    });
    //end选择明细的列表

    //begin添加/编辑对话框
    $("#recruitmentPlan_dialog").dialog({
        width: 500,
        height: 300,
        buttons: "#recruitmentPlan_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin处理功能方法的对象
    var obj = {
        //begin添加按钮
        add: function () {
            $("#recruitmentPlan_form").form("clear");
            $("#recruitmentPlan_dialog").dialog("setTitle", "计划添加");
            $("#recruitmentPlan_dialog").dialog("open");
        },
        //end添加按钮

        //begin查看按钮
        xxx: function () {

            var rows = $('#employeeNeeds').datagrid('getRows');

            for (var i = rows.length; i > 0; i--) {
                $('#employeeNeeds').datagrid('deleteRow', i - 1);
            }
            $("#recruitmentPlan_form").form("clear");
            $("#recruitmentPlan_dialog").dialog("setTitle", "用人需求编辑");

            var row = $("#recruitmentPlan_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            var recruitmentPlanId = row.id;
            $.get("/employeeNeeds/selectEmployeeNeedsByRecruitmentPlanId",
                {recruitmentPlanId: recruitmentPlanId},
                function (data) {
                    for (var i = 0; i < data.length; i++) {
                        console.log(data[0]);
                        var inputUser = data[i].inputUser;
                        var department = data[i].department;
                        var applicant = data[i].applicant;
                        var begindate = data[i].begindate;
                        var enddate = data[i].enddate;
                        var inputdate = data[i].inputdate;
                        $("#employeeNeeds").datagrid('insertRow', {
                            index: i,
                            row: {
                                inputUser: inputUser,
                                department: department,
                                applicant: applicant,
                                begindate: begindate,
                                enddate: enddate,
                                inputdate: inputdate
                            }
                        });
                    }
                });
            //employeeNeedsBill表格属性设置
            $("#recruitmentPlan_form").form("load", row);
            $("#recruitmentPlan_dialog").dialog("open");
        },
        //end查看按钮

        //begin取消按钮
        cancel: function () {
            $("#recruitmentPlan_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var recruitmentPlanId = $("#recruitmentPlanId").val();
            var url;
            if (recruitmentPlanId) {
                url = "/recruitmentPlan/update"
            } else {
                url = "/recruitmentPlan/save"
            }
            $("#recruitmentPlan_form").form("submit", {
                url: url,
                onSubmit: function (param) {
                    //獲取所有的id值,赋值到employeeNeedsIds中
                    var ids = $("#employeeNeeds").datagrid("getSelections");
                    console.log(ids);
                    for (var i = 0; i < ids.length; i++) {
                        param["employeeNeedsIds[" + i + "]"] = ids[i].id;
                    }
                },
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#recruitmentPlan_dialog").dialog("close");
                        $("#recruitmentPlan_datagrid").datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮

        //begin刷新按钮
        reload: function () {
            $("#recruitmentPlan_datagrid").datagrid("reload")
        }
        //end刷新按钮
    };
    //end处理功能方法的对象

    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

});